package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import classes.ClassUser;

import dao.UserDao;

/**
 * Servlet implementation class CadastroUser
 */
@WebServlet("/CadastroUser")
public class CadastroUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CadastroUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		ClassUser user = new ClassUser();
		user.setNome(request.getParameter("name"));
		user.setNickname(request.getParameter("nickname"));
		user.setConta(request.getParameter("email"));
		user.setSenha(request.getParameter("senhaconfirmada"));
		user.setSenha(request.getParameter("senha"));
		user.setCurso(request.getParameter("curso"));
		user.setInstituicao(request.getParameter("escola"));

		UserDao dao = new UserDao();
		String resultado = dao.submetCadastro(user);
       
        	if (resultado.equals("")) {
			RequestDispatcher rqd = request.getRequestDispatcher("Cadastrohorario.jsp");
			rqd.forward(request, response);
		}

		else {
			if (resultado.equals("E-mail e Nickname ")||resultado.equals("E-mail")||resultado.equals("Nickname")||resultado.equals("Erro no servidor. Cadastro"))
			request.setAttribute("mensagem", resultado + "  indisponivel(is)!"); 
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("Cadastro.jsp");
			dispatcher.forward(request, response);

		}
	}

}
